﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.Contacts;
using Google.GData.Client;
using Google.GData.Contacts;
using Google.GData.Extensions;
namespace Gmail
{
    public class Connector
    {
        public Connector(String Username, String Password) {
            this.Username = Username;
            this.Password = Password;

        }
        private String Username;
        private String Password;

        public Boolean IsLoginGood()
        {
            List<EmailEntry> Results = new List<EmailEntry>();
            RequestSettings RS = new RequestSettings("GmailAPI", Username, Password);
            ContactsRequest CR = new ContactsRequest(RS);

            Feed<Contact> Feed = CR.GetContacts();
            try
            {
                Feed.Entries.First();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public List<EmailEntry> GetEmails() 
        {
            List<EmailEntry> Results = new List<EmailEntry>();
            RequestSettings RS = new RequestSettings("GmailAPI", Username, Password);
            ContactsRequest CR = new ContactsRequest(RS);
            
            Feed<Contact> Feed = CR.GetContacts();
            foreach (Contact Contact in Feed.Entries) //Might throw InvalidCredentalsException
            {
                foreach (EMail Email in Contact.Emails)
                {
                    EmailEntry EmailEntry = new EmailEntry(Contact.Title, Email.Address);
                    Results.Add(EmailEntry);
                }
            }
            return Results;
        }
        


    }
}
